package com.example.server.service.impl;

import com.example.server.mapper.MyMenuMapper;
import com.example.server.mapper.MyRoleMapper;
import com.example.server.pojo.MyMenu;
import com.example.server.pojo.MyRole;
import com.example.server.service.MyRoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;

@Service
public class MyRoleServiceImpl implements MyRoleService {
    @Autowired
    private MyRoleMapper myRoleMapper;

    @Autowired
    private MyMenuMapper myMenuMapper;

    @Override
    public List<MyRole> allRole() {
        List<MyRole> list = myRoleMapper.allRole();
        return list;
    }

    @Override
    public List<MyRole> querylikeRole(String name) {
        return null;
    }

    @Override
    public List<MyRole> roleOfPower(Long roleId) {
        List<MyRole> list = myRoleMapper.roleOfPower(roleId);
        list.forEach(myRole -> {
            if (myRole.getSecondRole() != null){
                Map map = JSON.parseObject(JSON.toJSONString(myRole), Map.class);
                System.out.println(map);
                String[] list1 = myRole.getSecondRole().split(",");
                ArrayList<MyMenu> list2 = new ArrayList<MyMenu>();
                for (String str: list1) {
                    MyMenu myMenu = myMenuMapper.myMenu(Integer.valueOf(str));
                    list2.add(myMenu);
                }
                map.put("children", list2);
                System.out.println(map);
            }
        });
        return list;
    }

    @Override
    public ArrayList<Map<Object, Object>> roleOfPower1(Long roleId) {
        List<MyRole> list = myRoleMapper.roleOfPower(roleId);
        ArrayList<Map<Object, Object>> listall = new ArrayList<>();
        list.forEach(myRole -> {
            if (myRole.getSecondRole() != null){
                Map<Object, Object> map = JSON.parseObject(JSON.toJSONString(myRole), Map.class);
                System.out.println(map);
                String[] list1 = myRole.getSecondRole().split(",");
                ArrayList<MyMenu> list2 = new ArrayList<MyMenu>();
                for (String str: list1) {
                    MyMenu myMenu = myMenuMapper.myMenu(Integer.valueOf(str));
                    list2.add(myMenu);
                }
                map.put("children", list2);
                listall.add(map);
            }
        });

        return listall;
    }

    @Override
    public Integer addRole(String name) {
        return null;
    }

    @Override
    public Integer updateRole(MyRole myRole) {
        return null;
    }

    @Override
    public Integer delRole(Long roleId) {
        Integer num = myRoleMapper.delRole(roleId);
        return num;
    }
}
